package org.wang.generalcode.modules.gen.controller;


import cn.hutool.core.io.IoUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wang.generalcode.modules.gen.pojo.entity.GenProjectModify;
import org.wang.generalcode.modules.gen.service.ProjectModifyService;
import org.wang.generalcode.modules.gen.util.ProjectUtils;
import org.wang.generalcode.support.mybaties.Condition;
import org.wang.generalcode.support.mybaties.Query;
import org.wang.generalcode.support.result.Result;
import org.wang.generalcode.support.utils.Func;


@RestController
@AllArgsConstructor
@RequestMapping("/project")
@Schema(description = "代码生成", name = "代码生成接口")
public class ProjectModifyController {

    private final ProjectModifyService projectModifyService;

    @ApiOperationSupport(order = 1)
    @Schema(description = "分页查询", example = "传入query")
    @GetMapping("page")
    public Result<IPage<GenProjectModify>> page(Query query) {
        IPage<GenProjectModify> page = projectModifyService.page(Condition.getPage(query));
        return Result.success(page);
    }

    @ApiOperationSupport(order = 3)
    @Schema(description = "查询详情", example = "")
    @GetMapping("/detail")
    public Result<GenProjectModify> get(@Parameter(name = "主键", required = true) @RequestParam Long id) {
        GenProjectModify entity = projectModifyService.getById(id);
        return Result.success(entity);
    }

    @ApiOperationSupport(order = 5)
    @Schema(description = "保存", example = "")
    @PostMapping("/save")
    public Result<String> save(@RequestBody GenProjectModify entity) {
        entity.setExclusions(ProjectUtils.EXCLUSIONS);
        entity.setModifySuffix(ProjectUtils.MODIFY_SUFFIX);
        return Result.judge(projectModifyService.save(entity));
    }

    @ApiOperationSupport(order = 6)
    @Schema(description = "修改", example = "")
    @PostMapping("/update")
    public Result<String> update(@RequestBody GenProjectModify entity) {
        return Result.judge(projectModifyService.updateById(entity));
    }

    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @Schema(description = "删除", example = "传入ids")
    public Result<String> remove(@Parameter(name = "主键集合", required = true) @RequestParam String ids) {
        return Result.judge(projectModifyService.removeBatchByIds(Func.toLongList(ids)));
    }

    /**
     * 源码下载
     */
    @PostMapping("/download")
    @ApiOperationSupport(order = 8)
    @Schema(description = "源码下载", example = "传入ids")
    public void download(@Parameter(name = "主键", required = true) @RequestParam Long id, HttpServletResponse response) throws Exception {
        // 项目信息
        GenProjectModify project = projectModifyService.getById(id);

        byte[] data = projectModifyService.download(project);

        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"" + project.getModifyProjectName() + ".zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IoUtil.write(response.getOutputStream(), false, data);
    }
}
